/**
 *<p>Copyright: Copyright (c) 2012</p> 
 *<p>Company: SCPII</p>
 *<p>Project:optimus-manage</p>
 *@author xiezhouyan
 *@Date 2013-5-16 下午9:58:31
 *@version 1.0
 *@description:
 */
package com.cybertron.ironhide.manage.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cybertron.ironhide.manage.common.base.ImageInfo;

public class ImageMagickUtil {

	public static final String OS_NAME = System.getProperty("os.name")
			.toLowerCase();

	public static final Boolean IS_WINDOWS = OS_NAME.indexOf("windows") != -1 ? true
			: false;

	public static final Boolean IS_LINUX = OS_NAME.indexOf("linux") != -1 ? true
			: false;

	private static final Logger logger = LoggerFactory
			.getLogger(ImageMagickUtil.class);

	private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

	public static ImageInfo indentify(File file) {
		String command = "identify -format {\\\"width\\\":%W,\\\"height\\\":%H,\\\"size\\\":\\\"%b\\\",\\\"colorspace\\\":\\\"%r\\\"} "
				+ file.getAbsolutePath();
		if (IS_WINDOWS) {
			command = "cmd /c " + command;
		}
		logger.debug("indentify run " + command);
		String result = run(command);
		ImageInfo imageInfo = null;
		try {
			imageInfo = OBJECT_MAPPER.readValue(result, ImageInfo.class);
			if (imageInfo != null) {
				imageInfo.setOrignFile(file);
			}
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return imageInfo;
	}

	public static void mogrify(ImageInfo imageInfo) {
		String command = "mogrify -colorspace RGB -quality 100 "
				+ imageInfo.getOrignFile().getAbsolutePath();
		if (IS_WINDOWS) {
			command = "cmd /c " + command;
		} else if (IS_LINUX) {
			command = "/usr/bin/" + command;
		}
		logger.debug("mogrify run " + command);
		run(command);
	}

	public static void convert(ImageInfo imageInfo, File destFile, int maxDim,
			Long imageCompare) {
		String command = "convert  -resize ";
		if (IS_WINDOWS) {
			command += "\"" + maxDim + ">\" ";
		} else if (IS_LINUX) {
			command += maxDim + "> ";
		}
		if (!imageInfo.isRGB()) {
			logger.debug("colorspace.... file "
					+ imageInfo.getOrignFile().getAbsolutePath() + " not RGB");
			command += " -colorspace RGB ";
		}
		if (imageCompare != null && imageInfo.getSize() > imageCompare) {
			logger.debug("imageCompare " + imageCompare + " file size "
					+ imageInfo.getSize());
			Long percent = Math.round(Double.valueOf(imageCompare)
					/ Double.valueOf(imageInfo.getSize()) * 100);
			String percentQuality = String.valueOf(percent < 1 ? 1 : percent)
					+ "%";
			command += " -quality " + percentQuality + " ";
		}
		command += " " + imageInfo.getOrignFile().getAbsolutePath();
		command += " " + destFile.getAbsolutePath();
		if (IS_WINDOWS) {
			command = "cmd /c " + command;
		} else if (IS_LINUX) {
			command = "/usr/bin/" + command;
		}
		logger.debug("convert run " + command);
		run(command);
	}

	public static void convert(ImageInfo imageInfo, File destFile, int maxDim) {
		convert(imageInfo, destFile, maxDim, null);
	}

	public static synchronized String run(String cmd) {
		Process process;
		StringBuffer sb = new StringBuffer();
		try {
			process = Runtime.getRuntime().exec(cmd);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					process.getInputStream()));
			String c = null;
			while ((c = in.readLine()) != null) {
				logger.debug(" prcess result" + c);
				sb.append(c);
			}
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

}
